package Game
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.graphics.BitmapSmoothingQuality;
	
	import spark.components.Group;
	import spark.filters.DropShadowFilter;
	import spark.primitives.BitmapImage;

	public class Turret extends Group
	{
		[Embed(source="../assets/models/tank.png")]
		private var _SheetClassData:Class;
		private var _SheetBitmapData:BitmapData;
		
		private var _color:uint;
		private var _maskBitmapData:BitmapData;
		private var _maskBitmapImage:BitmapImage;
		
		private var _detailBitmapData:BitmapData;
		private var _detailBitmapImage:BitmapImage;
		
		private var _filter:DropShadowFilter = new DropShadowFilter();
		
		public static var SIMPLE_ARM:uint= 0;
		public static var DOUBLE_ARM:uint= 1;
		
		private var _arm:uint;
		
		public function Turret()
		{
			_color = Tank.DEFAULT_COLOR;
			_arm = SIMPLE_ARM;
			
			_SheetBitmapData = new _SheetClassData().bitmapData;
			
			// turret+arm mask
			_maskBitmapData = new BitmapData(26, 45+23, true, 0x000000);
			_maskBitmapData.copyPixels(_SheetBitmapData,new Rectangle(20, 60, 20, 45), new Point(3,0));
			_maskBitmapData.copyPixels(_SheetBitmapData,new Rectangle(26, 105, 26, 23), new Point(0,45));
			
			var _maskBitmapImage:BitmapImage = new BitmapImage();
			_maskBitmapImage.source = new Bitmap(_maskBitmapData);
			_maskBitmapImage.smooth = true;
			_maskBitmapImage.smoothingQuality = BitmapSmoothingQuality.HIGH;
			
			_maskBitmapImage.x=-13;
			_maskBitmapImage.y=-53;
			
			// turret+arm detail
			_detailBitmapData = new BitmapData(26, 45+23, true, 0x000000);
			_detailBitmapData.copyPixels(_SheetBitmapData,new Rectangle(0, 60, 20, 45), new Point(3,0));
			_detailBitmapData.copyPixels(_SheetBitmapData,new Rectangle(0, 105, 26, 23), new Point(0,45));
			
			_detailBitmapImage = new BitmapImage();
			_detailBitmapImage.source = new Bitmap(_detailBitmapData);
			_detailBitmapImage.smooth = true;
			_detailBitmapImage.smoothingQuality = BitmapSmoothingQuality.HIGH;
			
			_detailBitmapImage.x=-13;
			_detailBitmapImage.y=-53;
			
			// attach mask + detail
			this.addElement(_maskBitmapImage);
			this.addElement(_detailBitmapImage);
			
			// apply shadow filter
			_filter.blurX = 4;
			_filter.blurY = 4;
			_filter.quality = 10;
			_filter.alpha = 50;
			_filter.angle = 45;
			_filter.color = 0x000000;
			_filter.distance = 8;
			_filter.inner = false;
			
			this.filters = [ _filter ];

		}
		
		public function set color(color:uint):void
		{
			_maskBitmapData.threshold(_maskBitmapData, _maskBitmapData.rect, new Point(0,0), "==", _color, color);
			_color = color;
		}
		
		public function get arm():uint
		{
			return _arm
		}
		
		public function set arm(value:uint):void
		{
			if (_arm!=value)
				switch(value)
				{
					case SIMPLE_ARM:
					{
						_maskBitmapData.copyPixels(_SheetBitmapData,new Rectangle(20, 60, 20, 45), new Point(3,0));
						_maskBitmapData.copyPixels(_SheetBitmapData,new Rectangle(26, 105, 26, 23), new Point(0,45));
						_maskBitmapData.threshold(_maskBitmapData, _maskBitmapData.rect, new Point(0,0), "==", Tank.DEFAULT_COLOR, _color);
						
						_detailBitmapData.copyPixels(_SheetBitmapData,new Rectangle(0, 60, 20, 45), new Point(3,0));
						_detailBitmapData.copyPixels(_SheetBitmapData,new Rectangle(0, 105, 26, 23), new Point(0,45));
						break;
					}
						
					case DOUBLE_ARM:
					{
						_maskBitmapData.copyPixels(_SheetBitmapData,new Rectangle(60, 60, 20, 45), new Point(3,0));
						_maskBitmapData.copyPixels(_SheetBitmapData,new Rectangle(26, 105, 26, 23), new Point(0,45));
						_maskBitmapData.threshold(_maskBitmapData, _maskBitmapData.rect, new Point(0,0), "==", Tank.DEFAULT_COLOR, _color);
						
						_detailBitmapData.copyPixels(_SheetBitmapData,new Rectangle(40, 60, 20, 45), new Point(3,0));
						_detailBitmapData.copyPixels(_SheetBitmapData,new Rectangle(0, 105, 26, 23), new Point(0,45));
						
						break;
					}
						
					default:
					{
						break;
					}
				}
		}
	}
}